// import com.alibaba.druid.DbType_ptr;
// import com.alibaba.druid.sql.SQLUtils;
// import com.alibaba.druid.sql.ast.*;
// import com.alibaba.druid.sql.ast.expr.*;
// import com.alibaba.druid.sql.visitor.SQLASTVisitor;

// import java.util.ArrayList;
// import java.util.List;

#include "SQLCreateViewStatement.h"
#include "../../visitor/SQLASTVisitor.h"
#include "../../../utils/instanceof.h"
#include "../../SQLUtils.h"
#include "../expr/SQLVariantRefExpr.h"
#include "SQLDDLStatement.h"
#include "SQLBlockStatement.h"
#include "SQLTableElement.h"
#include "../expr/SQLCharExpr.h"

SQLCreateViewStatement_Level_ptr SQLCreateViewStatement_Level::CASCADED = SQLCreateViewStatement_Level_ptr(new SQLCreateViewStatement_Level(1 << 0, make_string_ptr("CASCADED")));
SQLCreateViewStatement_Level_ptr SQLCreateViewStatement_Level::LOCAL = SQLCreateViewStatement_Level_ptr(new SQLCreateViewStatement_Level(1 << 1, make_string_ptr("LOCAL")));

SQLCreateViewStatement_Level::SQLCreateViewStatement_Level(uint64_t mask, string_ptr name)
{
  this->mask = mask;
  this->name = name;
}

SQLExpr_ptr SQLCreateViewStatement_Column::getExpr()
{
  return expr;
}

void SQLCreateViewStatement_Column::setExpr(SQLExpr_ptr expr)
{
  if (expr != nullptr)
  {
    expr->setParent(SharedObject(SQLCreateViewStatement_Column));
  }
  this->expr = expr;
}

SQLCharExpr_ptr SQLCreateViewStatement_Column::getComment()
{
  return comment;
}

void SQLCreateViewStatement_Column::setComment(SQLCharExpr_ptr comment)
{
  if (comment != nullptr)
  {
    comment->setParent(SharedObject(SQLCreateViewStatement_Column));
  }
  this->comment = comment;
}

// @Override
void SQLCreateViewStatement_Column::accept0(SQLASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLCreateViewStatement_Column)))
  {
    acceptChild(visitor, expr);
    acceptChild(visitor, comment);
  }
}

SQLCreateViewStatement::SQLCreateViewStatement()
{
  columns = std::make_shared<std::list<SQLTableElement_ptr>>();
}

SQLCreateViewStatement::SQLCreateViewStatement(DbType_ptr dbType)
    : SQLStatementImpl(dbType)
{
  columns = std::make_shared<std::list<SQLTableElement_ptr>>();
}

string_ptr SQLCreateViewStatement::computeName()
{
  if (tableSource == nullptr)
  {
    return nullptr;
  }

  SQLExpr_ptr expr = tableSource->getExpr();
  if (instanceof <SQLExpr, SQLName>(expr))
  {
    string_ptr name = (std::dynamic_pointer_cast<SQLName>(expr))->getSimpleName();
    return SQLUtils::normalize(name);
  }

  return nullptr;
}

string_ptr SQLCreateViewStatement::getSchema()
{
  SQLName_ptr name = getName();
  if (name == nullptr)
  {
    return nullptr;
  }

  if (instanceof <SQLName, SQLPropertyExpr>(name))
  {
    return (std::dynamic_pointer_cast<SQLPropertyExpr>(name))->getOwnernName();
  }

  return nullptr;
}

BOOL_ptr SQLCreateViewStatement::isOrReplace()
{
  return orReplace;
}

void SQLCreateViewStatement::setOrReplace(BOOL_ptr orReplace)
{
  this->orReplace = orReplace;
}

SQLName_ptr SQLCreateViewStatement::getName()
{
  if (tableSource == nullptr)
  {
    return nullptr;
  }

  return std::dynamic_pointer_cast<SQLName>(tableSource->getExpr());
}

void SQLCreateViewStatement::setName(SQLName_ptr name)
{
  this->setTableSource(SQLExprTableSource_ptr(new SQLExprTableSource(name)));
}

void SQLCreateViewStatement::setName(string_ptr name)
{
  this->setName(SQLIdentifierExpr_ptr(new SQLIdentifierExpr(name)));
}

SQLExprTableSource_ptr SQLCreateViewStatement::getTableSource()
{
  return tableSource;
}

void SQLCreateViewStatement::setTableSource(SQLExprTableSource_ptr tableSource)
{
  if (tableSource != nullptr)
  {
    tableSource->setParent(SharedObject(SQLCreateViewStatement));
  }
  this->tableSource = tableSource;
}

BOOL_ptr SQLCreateViewStatement::isWithCheckOption()
{
  return withCheckOption;
}

void SQLCreateViewStatement::setWithCheckOption(BOOL_ptr withCheckOption)
{
  this->withCheckOption = withCheckOption;
}

BOOL_ptr SQLCreateViewStatement::isWithCascaded()
{
  return withCascaded;
}

void SQLCreateViewStatement::setWithCascaded(BOOL_ptr withCascaded)
{
  this->withCascaded = withCascaded;
}

BOOL_ptr SQLCreateViewStatement::isWithLocal()
{
  return withLocal;
}

void SQLCreateViewStatement::setWithLocal(BOOL_ptr withLocal)
{
  this->withLocal = withLocal;
}

BOOL_ptr SQLCreateViewStatement::isWithReadOnly()
{
  return withReadOnly;
}

void SQLCreateViewStatement::setWithReadOnly(BOOL_ptr withReadOnly)
{
  this->withReadOnly = withReadOnly;
}

SQLSelect_ptr SQLCreateViewStatement::getSubQuery()
{
  return subQuery;
}

void SQLCreateViewStatement::setSubQuery(SQLSelect_ptr subQuery)
{
  if (subQuery != nullptr)
  {
    subQuery->setParent(SharedObject(SQLCreateViewStatement));
  }
  this->subQuery = subQuery;
}

SQLTableElement_list_ptr SQLCreateViewStatement::getColumns()
{
  return columns;
}

void SQLCreateViewStatement::addColumn(SQLTableElement_ptr column)
{
  if (column != nullptr)
  {
    column->setParent(SharedObject(SQLCreateViewStatement));
  }
  this->columns->push_back(column);
}

BOOL_ptr SQLCreateViewStatement::isIfNotExists()
{
  return ifNotExists;
}

void SQLCreateViewStatement::setIfNotExists(BOOL_ptr ifNotExists)
{
  this->ifNotExists = ifNotExists;
}

SQLLiteralExpr_ptr SQLCreateViewStatement::getComment()
{
  return comment;
}

void SQLCreateViewStatement::setComment(SQLLiteralExpr_ptr comment)
{
  if (comment != nullptr)
  {
    comment->setParent(SharedObject(SQLCreateViewStatement));
  }
  this->comment = comment;
}

string_ptr SQLCreateViewStatement::getAlgorithm()
{
  return algorithm;
}

void SQLCreateViewStatement::setAlgorithm(string_ptr algorithm)
{
  this->algorithm = algorithm;
}

SQLName_ptr SQLCreateViewStatement::getDefiner()
{
  return definer;
}

void SQLCreateViewStatement::setDefiner(SQLName_ptr definer)
{
  if (definer != nullptr)
  {
    definer->setParent(SharedObject(SQLCreateViewStatement));
  }
  this->definer = definer;
}

string_ptr SQLCreateViewStatement::getSqlSecurity()
{
  return sqlSecurity;
}

void SQLCreateViewStatement::setSqlSecurity(string_ptr sqlSecurity)
{
  this->sqlSecurity = sqlSecurity;
}

BOOL_ptr SQLCreateViewStatement::isForce()
{
  return force;
}

void SQLCreateViewStatement::setForce(BOOL_ptr force)
{
  this->force = force;
}

// @Override
void SQLCreateViewStatement::accept0(SQLASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLCreateViewStatement)))
  {
    if (tableSource != nullptr)
    {
      tableSource->accept(visitor);
    }

    auto it_columns = columns->begin();
    for (int i = 0; i < columns->size(); i++)
    {
      std::advance(it_columns, i);
      SQLTableElement_ptr column = *it_columns;
      if (column != nullptr)
      {
        column->accept(visitor);
      }
    }

    if (comment != nullptr)
    {
      comment->accept(visitor);
    }

    if (subQuery != nullptr)
    {
      subQuery->accept(visitor);
    }
  }
  visitor->endVisit(SharedObject(SQLCreateViewStatement));
}

SQLObject_list_ptr SQLCreateViewStatement::getChildren()
{
  SQLObject_list_ptr children = std::make_shared<std::list<SQLObject_ptr>>();
  if (tableSource != nullptr)
  {
    children->push_back(tableSource);
  }
  // children.addAll(this->columns);
  for (auto it : *this->columns)
  {
    children->push_back(it);
  }
  if (comment != nullptr)
  {
    children->push_back(comment);
  }
  if (subQuery != nullptr)
  {
    children->push_back(subQuery);
  }
  return children;
}

BOOL_ptr SQLCreateViewStatement::isOnCluster()
{
  return onCluster;
}

void SQLCreateViewStatement::setOnCluster(BOOL_ptr onCluster)
{
  this->onCluster = onCluster;
}

SQLName_ptr SQLCreateViewStatement::getTo()
{
  return to;
}

void SQLCreateViewStatement::setTo(SQLName_ptr x)
{
  if (x != nullptr)
  {
    x->setParent(SharedObject(SQLCreateViewStatement));
  }
  this->to = x;
}

SQLVariantRefExpr_ptr SQLCreateViewStatement::getReturns()
{
  return returns;
}

void SQLCreateViewStatement::setReturns(SQLVariantRefExpr_ptr x)
{
  if (x != nullptr)
  {
    x->setParent(SharedObject(SQLCreateViewStatement));
  }
  this->returns = x;
}

SQLDDLStatement::DDLObjectType SQLCreateViewStatement::getReturnsDataType()
{
  return returnsDataType;
}

void SQLCreateViewStatement::setReturnsDataType(SQLDDLStatement::DDLObjectType x)
{
  // if (x != nullptr)
  // {
  // x->setParent(SharedObject(SQLCreateViewStatement));
  // }
  this->returnsDataType = x;
}

SQLBlockStatement_ptr SQLCreateViewStatement::getScript()
{
  return script;
}

void SQLCreateViewStatement::setScript(SQLBlockStatement_ptr x)
{
  if (x != nullptr)
  {
    x->setParent(SharedObject(SQLCreateViewStatement));
  }
  this->script = x;
}

SQLDDLStatement::DDLObjectType SQLCreateViewStatement::getDDLObjectType()
{
  return SQLDDLStatement::DDLObjectType::VIEW;
}

SQLObject_ptr SQLCreateViewStatement::clone()
{
  SQLObject_ptr clone_tmp = SQLCreateViewStatement_ptr(new SQLCreateViewStatement());
  SQLCreateViewStatement_ptr x = std::dynamic_pointer_cast<SQLCreateViewStatement>(clone_tmp);
  // SQLCreateViewStatement x = new SQLCreateViewStatement();

  x->orReplace = orReplace;
  x->force = force;
  if (subQuery != nullptr)
  {
    x->setSubQuery(std::dynamic_pointer_cast<SQLSelect>(subQuery->clone()));
  }
  x->ifNotExists = ifNotExists;

  x->algorithm = algorithm;
  if (definer != nullptr)
  {
    x->setDefiner(std::dynamic_pointer_cast<SQLName>(definer->clone()));
  }
  x->sqlSecurity = sqlSecurity;
  if (tableSource != nullptr)
  {
    x->setTableSource(std::dynamic_pointer_cast<SQLExprTableSource>(tableSource->clone()));
  }
  for (SQLTableElement_ptr column : *columns)
  {
    SQLTableElement_ptr column2 = std::dynamic_pointer_cast<SQLTableElement>(column->clone());
    column2->setParent(x);
    x->columns->push_back(column2);
  }
  x->withCheckOption = withCheckOption;
  x->withCascaded = withCascaded;
  x->withLocal = withLocal;
  x->withReadOnly = withReadOnly;

  if (comment != nullptr)
  {
    x->setComment(std::dynamic_pointer_cast<SQLLiteralExpr>(comment->clone()));
  }

  x->onCluster = onCluster;
  if (x->to != nullptr)
  {
    to = std::dynamic_pointer_cast<SQLName>(x->to->clone());
  }

  if (x->returns != nullptr)
  {
    returns = std::dynamic_pointer_cast<SQLVariantRefExpr>(x->returns->clone());
  }

  // if (x->returnsDataType != nullptr)
  // {
  returnsDataType = x->returnsDataType;
  // }

  return x;
}
